package com.boc.ewr.bcimp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boc.ewr.bcimp.entity.QccBusInfo;
import com.boc.ewr.bcimp.mapper.QccBusInfoMapper;
import com.boc.ewr.bcimp.service.IQccBusInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 企查查-工商信息
 * @Author: jeecg-boot
 * @Date:   2023-04-20
 * @Version: V1.0
 */
@Service
public class QccBusInfoServiceImpl extends ServiceImpl<QccBusInfoMapper, QccBusInfo> implements IQccBusInfoService {

    @Autowired
    QccBusInfoMapper qccBusInfoMapper;

    //批量导入
    @Transactional
    public boolean initSaveBatch(Collection<QccBusInfo> entityList, Boolean flag){
        if(entityList.isEmpty() || entityList.size() == 0){
            return false;
        }
        //以最后一次导入为准，不变更不替代原数据
        List<QccBusInfo> list = list();
        entityList.addAll(list);

        entityList = entityList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(o -> o.getCoreNm() ))), ArrayList::new));//去重，账户号/文书合同号

        //整理好数据后，清空所有记录
        if (flag) {
            qccBusInfoMapper.delete(null);
        }
        // 再统一插入数据
        return saveBatch(entityList);
    }
}
